﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AbstraX;
using SolutionFramework.Entities;
using AbstraX.Contracts;
using AbstraX.ClientInterfaces;
using System.Diagnostics;
using SLControlLibrary;

namespace SilverlightTestClient
{
    public partial class MainPage : UserControl
    {
        private ThreadSafeLog.Log log;
        private UIThread uiThread;

        public MainPage()
        {
            uiThread = new UIThread(this.Dispatcher);
            InitializeComponent();
        }

        public void AddOutput(string output)
        {
            uiThread.Run(() =>
            {
                var textBlock = new TextBlock();

                textBlock.Text = output;

                OutputListBox.Items.Add(textBlock);
                OutputListBox.ScrollIntoView(textBlock);
            });
        }

        private void OnInternalError(Exception exception)
        {
            //AddOutput(exception.Message);
        }

        private void OnInternalMessage(string message)
        {
            //AddOutput(message);
        }

        private void OnGeneralMessage(string message)
        {
            //AddOutput(message);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            var context = new SolutionFrameworkDomainContext();
            var query = context.GetProviderListQuery();

            AddOutput("Initializing...");

            ThreadSafeLog.Log.Enabled = true;

            log = new ThreadSafeLog.Log(this);

            ProviderClientManager.StatusLabel = StatusBar.Left;
            ProviderClientManager.SetStatus("Initializing...");
            ProviderClientManager.OnInternalError += new OnInternalErrorHandler(OnInternalError);
            ProviderClientManager.OnInternalMessage += new OnInternalMessageHandler(OnInternalMessage);
            ProviderClientManager.OnGeneralMessage += new OnGeneralMessageHandler(OnGeneralMessage);

            ProviderClientManager.Initialize(log);

            var operation = context.Load<ProviderListItem>(query);

            operation.Completed += (sender2, e2) =>
            {
                ProviderClientManager.LoadProviders(operation.Entities);

                ProviderClientManager.OnRootsLoaded += (sender3, e3) =>
                {
                    var operation2 = ProviderClientManager.GenerateItemFromID<IAttribute>("/AssembliesRoot[@URL='AbstraX://MCLaptop64/AssembliesRoot']/Assembly[@Assembly='System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089']/AssemblyType[@Type='System.Data.EntityCommandCompilationException']/PropertyAttribute[@Property='Message']");

                    operation2.Generated += (sender4, e4) =>
                    {
                        var attribute1 = e4.Item;

                        var operation3 = ProviderClientManager.GenerateItemFromID<IAttribute>("/EntitiesRoot[@URL='AbstraX://MCLaptop64/EntitiesRoot']/Solution[@Solution='SolutionFramework']/Project[@Project='SolutionFramework.Entities']/Model[@Model='Model1.edmx']/Entity_Container[@Container='SolutionFrameworkEntities3']/Entity_Set[@EntitySet='ViewInstances']/EntityType[@Entity='ViewInstance']/EntityProperty[@Property='InstanceID']");

                        operation3.Generated += (sender5, e5) =>
                        {
                            var dataType1 = attribute1.DataType;
                            var attribute2 = e5.Item;
                            var dataType2 = attribute2.DataType;

                            if (dataType1 == null || dataType2 == null)
                            {
                                Debugger.Break();
                            }
                        };

                        operation2.ExceptionOccured += (sender5, e5) =>
                        {
                            Debugger.Break();
                        };
                    };

                    operation2.ExceptionOccured += (sender4, e4) =>
                    {
                        Debugger.Break();
                    };
                };

            };
        }
    }
}
